package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.VideoAdapter;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.MoreVideoListTask;
import com.xgkj.diyiketang.task.SearchVideoTask;
import com.xgkj.diyiketang.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/29.
 */
public class MoreSellerVideoActivity extends BaseActivity{
    private Context mContext;
    private PullToRefreshGridView mListView;
    private VideoAdapter mAdapter;
    private int moreVideoTaskId = -1;
    private int searchTaskId = -1;
    private int size = 10;
    private int currentPage = 1;
    private String typeId;
    private String keyword;
    private String from;
    private TextView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.morevideolist);
        mContext = this;
        initView();
        Intent intent = getIntent();
        String title = intent.getStringExtra("TITLE");
        TextView titleText = (TextView) findViewById(R.id.title);
        if (!TextUtils.isEmpty(title)) {
            titleText.setText(title);
        }
        from = intent.getStringExtra(ProtocolConst.MORE_FROM);
        if(from != null && from.equals(ProtocolConst.FORM_SEARCH)){
            keyword = intent.getStringExtra("keyword");
            currentPage = 1;
            doSearch();
        }else {
            typeId = intent.getStringExtra("TYPEID");
            currentPage = 1;
            getMoreVieoList();
        }
    }

    private void doSearch(){
        if(currentPage == 0){
            mListView.onRefreshComplete();
            mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            ToastUtil.showMessage(mContext,R.string.no_more);
            return;
        }
        SearchVideoTask task = new SearchVideoTask(keyword,20,currentPage);
        task.addListener(new MoreListner(mContext));
        searchTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void  getMoreVieoList(){
        if(currentPage == 0){
            mListView.onRefreshComplete();
            mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            ToastUtil.showMessage(mContext, R.string.no_more);
            return;
        }
        if(moreVideoTaskId != -1){
            return;
        }
        MoreVideoListTask task = new MoreVideoListTask(size,currentPage,typeId,null,null,null);
        task.addListener(new MoreListner(mContext));
        moreVideoTaskId = getEatShowApp().getTaskManager().addTask(task);

    }

    class MoreListner extends ShowToastTaskEventListener{

        public MoreListner(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == moreVideoTaskId){
                moreVideoTaskId = -1;
                mListView.onRefreshComplete();
                if (currentPage == 1){
                    mAdapter.clearData();
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }
                List<VideoInfo> attentionList = new ArrayList<VideoInfo>();
                if(data != null){
                    attentionList = (List<VideoInfo>) data;
                }

                if(attentionList != null && attentionList.size()>0){
                    mAdapter.updateData(attentionList);
                }

                currentPage = result;
            }else if(taskId == searchTaskId){
                List<VideoInfo> searchList = new ArrayList<VideoInfo>();
                if(data != null){
                    mListView.onRefreshComplete();
                    if (currentPage == 1){
                        mAdapter.clearData();
                        mListView.setMode(PullToRefreshBase.Mode.BOTH);
                    }
                    searchList = (List<VideoInfo>) data;
                    if(searchList != null && searchList.size() > 0) {
                        mAdapter.clearData();
                        mAdapter.updateData(searchList);
                    }
                    currentPage = result;
                }
            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == moreVideoTaskId){
                moreVideoTaskId = -1;
                mListView.onRefreshComplete();
            }else if(taskId == searchTaskId){
                searchTaskId = -1;
                mListView.onRefreshComplete();
            }
        }
    }
    private void initView() {
        back= (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView = (PullToRefreshGridView) findViewById(R.id.more_list);
        mAdapter = new VideoAdapter(mContext);
        mListView.setAdapter(mAdapter);
        mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                currentPage = 1;
                if(from != null && from.equals(ProtocolConst.FORM_SEARCH)){
                    doSearch();
                }else{
                    getMoreVieoList();
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                if(from != null && from.equals(ProtocolConst.FORM_SEARCH)){
                    doSearch();
                }else{
                    getMoreVieoList();
                }
            }
        });
    }
}
